package pers.cnzdy.tutorial.Chapter8_Message_Service.Section2_Notification;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.net.Uri;
import android.os.Build;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
//import android.support.v7.app.NotificationCompat;
import android.view.View;

import java.io.File;

import pers.cnzdy.tutorial.Chapter5_UI.Section1_UIDesign.QuizActivity;
import pers.cnzdy.tutorial.R;

public class NotificationActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_notification);
        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        manager.cancel(1);
    }

    public void clickNotification(View source) {
        Intent intent = new Intent(this, QuizActivity.class);
        PendingIntent pi = PendingIntent.getActivity(this, 0, intent,
                PendingIntent.FLAG_CANCEL_CURRENT);

        NotificationManager manager = (NotificationManager) getSystemService (NOTIFICATION_SERVICE);
        Notification notification;
        Notification.Builder builder;

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            builder = new Notification.Builder(this,"5996773");
        }else {
            builder = new Notification.Builder(this);
        }

        builder.setContentTitle("新的测试题目")
                .setContentText("Android应用界面中UI（User Interface）组件间有严格的层次关系，有哪两种类型的视图组件？")
                .setWhen(System.currentTimeMillis())
                .setSmallIcon(R.mipmap.ic_launcher)
                .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
                .setContentIntent(pi)
                .setAutoCancel(true)
                .setSound(Uri.fromFile(new File("/system/media/audio/ringtones/Andromeda.ogg")))
                .setVibrate(new long[] {0, 500, 500, 1000})
                .build();

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel("5996773",
                    "安卓10a", NotificationManager.IMPORTANCE_DEFAULT);
            channel.enableLights(true);           // 是否在桌面icon右上角展示小红点
            channel.setLightColor(Color.GREEN);   // 小红点颜色
            channel.setShowBadge(false);          // 是否在久按桌面图标时显示此渠道的通知
            manager.createNotificationChannel(channel);
        }

        notification = builder.build();
        manager.notify(1, notification);
    }
}